package com.cg.managesystem.controller;

import com.cg.managesystem.common.JSONResult;
import com.cg.managesystem.entity.Room;
import com.cg.managesystem.entity.User;
import com.cg.managesystem.service.RoomService;
import com.cg.managesystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Create tao on 2019/8/14
 */

@RestController
@RequestMapping("/manage")
public class ManageController {

    @Autowired
    UserService userService;

    @Autowired
    private RoomService roomService;


    /**
     * 用户管理模块，根据用户id查询用户信息
     *
     * @param uid
     * @return
     */
    @RequestMapping(value = "/select/{uid}", method = RequestMethod.GET)
    public User selectUserById(@PathVariable("uid") Integer uid) {
        return userService.selectUserById(uid);
    }


    /**
     * 用户管理模块，根据用户id删除用户信息
     *
     * @param uid
     * @return
     */
    @RequestMapping(value = "/delete/{uid}", method = RequestMethod.DELETE)
    public String deleteUserById(@PathVariable("uid" +
            "") Integer uid) {
        int i = userService.deleteUserById(uid);
        String msg = (i == 1) ? "删除成功" : "删除失败";
        return msg;
    }


    /**
     * 测试方法，用来检验路径是否正确
     *
     * @return "hello"
     */
    @PostMapping(value = {"/", ""})
    public String hello() {
        return "welcome to cgManageSystem";
    }

    /**
     * 查询所有房间信息
     *
     * @return rooms
     */
    @PostMapping(value = "/findAllRooms")
    public List<Room> findAllRooms() {
        List<Room> rooms = roomService.findAllRooms();
        return rooms;
    }

    /**
     * 添加一个房间
     *
     * @param room
     * @return
     */
    @PostMapping(value = "/addRoom")
    public JSONResult addRoom(Room record,
                              @RequestParam String room,
                              @RequestParam String type
    ) {
        record.setRoom(room);
        record.setType(type);
        record.setStatus("0");
        record.setStars(5.0);
        record.setDescription("五星级的家");
        int result = roomService.addRoom(record);
        if (result != 0) {
            return JSONResult.ok("添加房间成功");
        } else {
            return JSONResult.errorMsg("添加房间失败");
        }
    }

    /**
     * 按rid删除房间
     *
     * @param rid
     * @return
     */
    @PostMapping(value = "/deleteRoomById")
    public int deleteRoomById(Integer rid) {
        return roomService.deleteRoomById(rid);
    }

    /**
     * 按rid查找一个房间
     *
     * @param id
     * @return
     */
    @PostMapping(value = "/findRoomById")
    public Room findRoomById(Integer id) {
        return roomService.findRoomById(id);
    }

    /**
     * 按ID更新一个房间
     *
     * @param room
     * @return
     */
    @PostMapping(value = "/updateRoomById")
    public int updateRoomById(Room room) {
        return roomService.updateRoomById(room);
    }


}
